package com.atlin.project.utils.json;

import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.commons.lang3.StringUtils;

/**
 * @author atlin
 * @description gson 属性过滤器
 * @createDate 2024/9/10 10:28
 */
public class GsonFilterUtils {

    /**
     * 排除指定的属性
     *
     * @param excludeParamNames 需要排除的字段名称
     * @return Gson
     */
    public static Gson propertyFilter(String[] excludeParamNames) {
        if (excludeParamNames == null || excludeParamNames.length == 0) {
            return new Gson();
        }
        GsonBuilder builder = new GsonBuilder();
        builder.setExclusionStrategies(new ExclusionStrategy() {
            @Override
            public boolean shouldSkipField(FieldAttributes fieldAttributes) {
                return StringUtils.equalsAny(fieldAttributes.getName(), excludeParamNames);
            }

            @Override
            public boolean shouldSkipClass(Class<?> aClass) {
                return false;
            }
        });
        return builder.create();
    }
}
